package com.lottery.material.controller.portal.competition;

import com.lottery.material.controller.base.BaseController;
import com.lottery.material.enumeration.ResultEnum;
import com.lottery.material.pojo.article.CompetitionInfo;
import com.lottery.material.pojo.page.PageData;
import com.lottery.material.resolver.CustomException;
import com.lottery.material.service.portal.competition.CompetitionInfoService;
import com.lottery.material.util.CalendarUtil;
import com.lottery.material.util.ExceptionUtil;
import com.lottery.material.util.ParameterUtil;
import com.lottery.material.util.ResponseResult;
import com.lottery.material.vo.portal.banner.BannerVo;
import com.lottery.material.vo.portal.competition.CompetitionArticleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author:houjie
 * @Description: 赛事接口
 * @Date:Create in 2019/4/8 16:50
 */
@Api(value = "赛事相关接口", tags = { "赛事相关接口" })
@Controller
@Slf4j
@RequestMapping("/portal/competition")
public class CompetitionController extends BaseController {

    @Autowired
    CompetitionInfoService competitionInfoService;

    @ApiOperation(value="获取赛事", notes="获取赛事")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "competitionType", value = "赛事类型（1：竞彩足球；2：竞彩篮球）", required = true, dataType = "String"),
            @ApiImplicitParam(name = "page", value = "用于分页", required = true, dataType = "String")
    })
    @RequestMapping("/getCompetition")
    @ResponseBody
    public ResponseResult getCompetition(HttpServletRequest request){
        Map<String, String> reqParamMap = new HashMap<>();
        //解析参数
        if (ParameterUtil.analyticParameter(request, reqParamMap)) {
            throw new CustomException(ResultEnum.PARSE_EXCEPTION);
        }

        //非空判断
        ResponseResult respDate = validateEmpty(reqParamMap);
        if (null != respDate) {
            return respDate;
        }

        String userId = reqParamMap.get("userId");
        if (StringUtils.isBlank(userId)) {
            return ResponseResult.buildFormatResult(ResultEnum.PARAMETER_NOT_NULL, "userId");
        }
        String competitionType = reqParamMap.get("competitionType");
        if(StringUtils.isBlank(competitionType)){
            return ResponseResult.buildResult("赛事类型不能为空！");
        }
        String page = reqParamMap.get("page");
        if (StringUtils.isBlank(page)) {
            return ResponseResult.buildResult(ResultEnum.PARAMETER_BLANK_ERROR.getCode(), String.format(ResultEnum.PARAMETER_BLANK_ERROR.getMessage(),"page"));
        }
        //签名验证
        ResponseResult responseResult = ParameterUtil.checkSign(reqParamMap);
        if(null != responseResult){
            return responseResult;
        }

        Map<String,Object> paramMap = new HashMap<>();
        paramMap.putAll(reqParamMap);
        paramMap.put("state",1);
        paramMap.put("endTime", CalendarUtil.dateToString(CalendarUtil.getWantedDate(CalendarUtil.getSystemCurrentDate(),null,2,null,null, null),"yyyy-MM-dd"));
        List<CompetitionInfo> competitionList = competitionInfoService.findAllByCompetitionTypeAndStatePage(paramMap);
        return ResponseResult.buildResult(200,"查询成功！",competitionList);
    }

    /**
     * description: 获取指定赛事信息
     * author sunny
     * createDate 2019-04-18 下午 8:22
     * @param request
     * @return
     */
    @RequestMapping(value = "/getCompetitionDetail", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult getCompetitionDetail(HttpServletRequest request) {
        Map<String, String> reqParamMap = new HashMap<>();
        //解析参数
        if (ParameterUtil.analyticParameter(request, reqParamMap)) {
            throw new CustomException(ResultEnum.PARSE_EXCEPTION);
        }

        //非空判断
        ResponseResult respDate = validateEmpty(reqParamMap);
        if (null != respDate) {
            return respDate;
        }

        String competitionId = reqParamMap.get("competitionId");
        if (StringUtils.isBlank(competitionId)) {
            return ResponseResult.buildFormatResult(ResultEnum.PARAMETER_NOT_NULL, "competitionId");
        }

        //签名验证
        ResponseResult responseResult = ParameterUtil.checkSign(reqParamMap);
        if (null != responseResult) {
            return responseResult;
        }

        try {
            //查询赛事信息
            CompetitionInfo competitionInfo = competitionInfoService.findByCompetitionId(Integer.valueOf(competitionId));
            if(Objects.isNull(competitionInfo)){
                return ResponseResult.buildResult(ResultEnum.GET_COMPETITION_DETAIL_FAIL);
            }
            //封装赛事VO
            CompetitionArticleVo competitionArticleVo = new CompetitionArticleVo();
            competitionArticleVo.setId(String.valueOf(competitionInfo.getId()));
            competitionArticleVo.setPlayingDate(competitionInfo.getPlayingDate());
            competitionArticleVo.setPlayingTime(competitionInfo.getPlayingTime());
            competitionArticleVo.setLeague(competitionInfo.getLeague());
            competitionArticleVo.setHomeTeam(competitionInfo.getHomeTeam());
            competitionArticleVo.setVisitingTeam(competitionInfo.getVisitingTeam());
            return ResponseResult.buildResult(ResultEnum.SUCCESS, competitionArticleVo);
        } catch (Exception e) {
            log.error("【获取指定赛事信息出现异常】:{}", ExceptionUtil.logExceptionStack(e));
            throw new CustomException(ResultEnum.GET_COMPETITION_DETAIL_EXCEPTION);
        }
    }
}
